<template>
  <ve-card
    :title="'车牌号：' + record.licenseNo"
    :sub-title="record.statusStr"
    :type="record.status == 0 ? 'yellow' : record.status == 1 ? 'blue' : 'gray'"
    @click="toDetail(record.id)"
  >
    <ve-cell label="维修单关联门店">{{ record.siteName || '-' }}</ve-cell>
    <ve-cell label="入厂时间">{{ record.arrivalTime || '-' }}</ve-cell>
    <ve-cell label="出厂时间">{{ record.leaveTime || '-' }}</ve-cell>
    <ve-cell label="关联订单号">
      <view>
        {{ record.orderNo || '-' }}
        <view class="line-ellipsis" v-if="record.giveStaffName" style="width: 400rpx">交车人：{{ record.giveStaffName }}</view>
      </view>
    </ve-cell>
    <ve-cell label="承租人信息">
      <div>
        <template v-if="record.memberName && record.memberPhone"> {{ record.memberName }} | {{ record.memberPhone }} </template>
        <template v-else>-</template>
      </div>
    </ve-cell>
    <ve-cell label="修理厂">{{ record.repairShop || '-' }}</ve-cell>
    <ve-cell-price label="费用金额" :value="record.repairAmount"></ve-cell-price>
    <ve-cell label="负责人">{{ record.undertaker || '-' }}</ve-cell>
    <ve-operate-list :operates="operates" :record="record" />
  </ve-card>

  <!-- 二次确认 -->
  <ve-confirm titleText="提示" v-model="confirmVisible" @confirm="platoonCarFn">
    <view class="confirmCont">
      <view>关闭后，不支持恢复，是否确认关闭？</view>
    </view>
  </ve-confirm>
</template>

<script setup lang="ts">
  import { userStore } from '@/stores'
  import { ref } from 'vue'
  import { closeApi } from '@/api/asset/car-repair'

  const userInfoStore = userStore()

  defineProps({
    record: {
      type: Object,
      default: () => {},
    },
  })

  const confirmVisible = ref(false)
  const currentClose = ref<any>({})
  const platoonCarFn = () => {
    closeApi({ id: currentClose.value.id }).then(() => {
      uni.showToast({
        title: '关闭成功',
        icon: 'success',
      })
      confirmVisible.value = false
      uni.$emit('loadData')
    })
  }

  const toDetail = (id) => {
    const permissions = userInfoStore.permissons
    if (permissions.includes('svmsapp:car-repair.detail')) {
      uni.navigateTo({
        url: '/pages/asset/car-repair/detail/index?id=' + id,
      })
    }
  }

  const operates = ref([
    // {
    //   label: '查看',
    //   permission: 'svmsapp:car-repair.detail',
    //   method: (record: any) => {
    //     uni.navigateTo({
    //       url: '/pages/asset/car-repair/detail/index?id=' + record.id
    //     })
    //   },
    // },
    {
      label: '编辑',
      permission: 'svmsapp:car-repair.edit',
      show: (record) => record.status == 0 || record.status == 1 || record.status == 2,
      method: (record: any) => {
        // 0 待维修 1 维修中 2 维修完成 7 已关闭
        if (record.status == 2) {
          uni.navigateTo({
            url: '/pages/asset/car-repair/form/index?id=' + record.id + '&pageType=finish',
          })
        } else if (record.status == 1) {
          uni.navigateTo({
            url: '/pages/asset/car-repair/form/index?id=' + record.id + '&pageType=start',
          })
        } else {
          uni.navigateTo({
            url: '/pages/asset/car-repair/form/index?id=' + record.id + '&pageType=' + '',
          })
        }
      },
    },
    {
      label: '开始维修',
      permission: 'svmsapp:car-repair.start',
      show: (record) => record.status == 0,
      method: (record: any) => {
        uni.navigateTo({
          url: '/pages/asset/car-repair/form/index?id=' + record.id + '&pageType=start',
        })
      },
    },
    {
      label: '完成维修',
      permission: 'svmsapp:car-repair.end',
      show: (record) => record.status == 1,
      method: (record: any) => {
        uni.navigateTo({
          url: '/pages/asset/car-repair/form/index?id=' + record.id + '&pageType=end',
        })
      },
    },
    {
      label: '费用信息',
      permission: 'svmsapp:car-repair.fee',
      show: (record: any) => record.status == 2,
      method: (record: any) => {
        uni.navigateTo({
          url: '/pages/asset/asset-fee/list?feeCategory=2&businesId=' + record.id,
        })
      },
    },
    {
      label: '关闭',
      permission: 'svmsapp:car-repair.close',
      show: (record: any) => record.status == 0 || record.status == 1,
      method: (record: any) => {
        currentClose.value = record
        confirmVisible.value = true
      },
    },
  ])
</script>

<style lang="less" scoped>
  .card-footer {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .card-footer {
      width: 100%;
      height: 100%;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      padding: 32rpx 0;

      .operate-list {
        width: 100%;
        font-size: 28rpx;
        font-weight: 400;
        color: #8491a8;
      }

      .ve-button {
        margin-left: 24rpx;
        margin-bottom: 24rpx;
      }
    }
  }
  :deep(.ve-cell-title) {
    width: 220rpx;
    max-width: 220rpx;
  }
</style>
